package com.zzyl.controller.web;

import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.*;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.entity.Device;
import com.zzyl.properties.AliIoTConfigProperties;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
public class DeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;
  @Autowired
  private AliIoTConfigProperties aliIoTConfigProperties;
  @Autowired
  private Client client;
    //从物联网平台同步产品列表
    @PostMapping("/iot/syncProductList")
    public ResponseResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }
    //查询所有产品列表
    @GetMapping("/iot/allProduct")
    public ResponseResult findAllProduct() {
        List<ProductVo> list = deviceService.findAllProduct();
        return success(list);
    }

    @PostMapping("/iot/RegisterDevice")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success();
    }

    //分页查询设备列表
    @GetMapping("/iot/pageQueryDevice")
    public ResponseResult pageQueryDevice(DevicePageQueryDto devicePageQueryDto) {
        PageResponse<Device> pageResponse = deviceService.findByPage(devicePageQueryDto);
        return success(pageResponse);
    }

    //查询设备详细数据
    @PostMapping("/iot/QueryDeviceDetail")
    public ResponseResult queryDeviceDetail(@RequestBody QueryDeviceDetailRequest request) {
        DeviceVo deviceVo = deviceService.queryDeviceDetail(request);
        return success(deviceVo);
    }
    // 删除设备
    @DeleteMapping("/iot/DeleteDevice")
    public ResponseResult deleteDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.delete(deviceDto);
        return success();
    }
    // 更新设备
    @PostMapping("/iot/UpdateDevice")
    public ResponseResult updateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return success();
    }
    //查看指定产品的已发布物模型中的功能定义详情
    @PostMapping("/iot/QueryThingModelPublished")
    public ResponseResult queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryThingModelPublishedResponse response = client.queryThingModelPublished(request);
        return ResponseResult.success(response.getBody().getData());
    }

    //查询指定设备属性状态
    @PostMapping("/iot/QueryDevicePropertyStatus")
    public ResponseResult QueryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryDevicePropertyStatusResponse deviceStatus = client.queryDevicePropertyStatus(request);
        return ResponseResult.success(deviceStatus.getBody().getData());
    }
}